package com.tkjt.pms.modules.report.service.impl;

import java.util.*;

import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.modules.scoring.mapper.TkScoringMapper;
import com.tkjt.pms.server.common.DepartUserChangeService;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecgframework.poi.excel.def.MapExcelConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tkjt.pms.modules.report.mapper.ReportDownLoadMapper;
import com.tkjt.pms.modules.report.service.ExcelBuilder;
import com.tkjt.pms.modules.report.service.ReportExcelService;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderParam;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderResult;
import com.tkjt.pms.modules.report.vo.DatagridVO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ReportExcelServiceImpl implements ReportExcelService {

	@Autowired
	private Map<String, ExcelBuilder> excelBuilders = new HashMap<String, ExcelBuilder>();
//	@Autowired
//	private TkIndexReportMapper tkIndexReportMapper;

	@Autowired
	private ReportDownLoadMapper reportDownLoadMapper;
	@Autowired
	private TkScoringMapper tkScoringMapper;

	@Autowired
	private DepartUserChangeService departUserChangeService;
	
	@Override
	public Map<String, Object> buildReportExcelData(boolean isTotal,String indexYearMonth, String reportCode,LoginUser loginUser,Map<String,String> detailConditions,DatagridVO datagridVO,String dataType) {
		log.info("ReportExcelService, indexYearMonth={}, reportCode={}", indexYearMonth, reportCode);
		
		Map<String,Object> dataMap = new HashMap<>();
		ExcelBuilderParam builderParam = new ExcelBuilderParam();
		builderParam.addParam(ExcelBuilderParam.INDEX_YEAR_MONTH, indexYearMonth);
		String indexGroup;
//		if(datagridVO.getIndexGroup().equals("1")){
//			indexGroup = "A01A01A02A";
//		}else{
//			indexGroup = "A01A01A01A";
//		}
		indexGroup = datagridVO.getIndexGroup();
		builderParam.addParam(ExcelBuilderParam.DATAGRIDVO, datagridVO);
		Map<String,String> titleMap = new HashMap<>();
		//TITLE_WEIGHT_DEPARTS集合
		List<String> weightDeparts = new ArrayList<>();
		//TITLE_DATA_DEPARTS集合
		List<String> dataDeparts = new ArrayList<>();
		/***************************************************
		 * *查找数据组建类
		 */
		ExcelBuilder builder = excelBuilders.get(reportCode);
		Map<String,Object> param = new HashMap<>();
		//非绩效考核报表code,调用我的绩效报表excel数据封装类
		if(builder == null){
			builder = excelBuilders.get("myReport");
			String[] reports = reportCode.split("@");
			List<String> orgCodes = reportDownLoadMapper.getOrgCodesByUserName(reports[0]);
			List<String> rcurrentUserRoleCodes = tkScoringMapper.selectRolesByUserName(reports[0]);
			if(!rcurrentUserRoleCodes.contains("ykgm") && !rcurrentUserRoleCodes.contains("ykaudit") && !rcurrentUserRoleCodes.contains("ykkpiowner")){
				param.put("indexScorer",reports[0]);
			}
			param.put("departCodes",orgCodes);
			List<String> reportOrgCodes = tkScoringMapper.selectReportOrgCodesByUserName(reports[0]);
			if(reportOrgCodes.size() > 0){
				param.put("reportOrgCodes",reportOrgCodes);
			}
		}else{
			param.put("indexGroup",indexGroup);
		}
		List<Map<String,Object>> weightDepartMaps = reportDownLoadMapper.getDepartByGroup(param);
		for(Map<String,Object> weightDepart : weightDepartMaps){
			weightDeparts.add(weightDepart.get("code").toString());
			titleMap.put(weightDepart.get("code").toString(),weightDepart.get("name").toString());
		}
		List<Map<String,Object>> dataDepartMaps = reportDownLoadMapper.getDepartUserByGroup(param);
		String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
		Map<String,Object> userDepartChangeMap = departUserChangeService.selectDepartUserChangeDataByMonth(indexYearMonth);
		for(Map<String,Object> dataDepartMap : dataDepartMaps){
			if(builder != null){
				if(currentYearMonth.equals(indexYearMonth)){
					titleMap.put(dataDepartMap.get("code").toString(),dataDepartMap.get("name").toString());
					dataDeparts.add(dataDepartMap.get("code").toString());
				}else{
					departUserChangeService.changeUserForLeaderReprot(userDepartChangeMap,titleMap,dataDeparts,dataDepartMap);
				}
			}else{
				if(currentYearMonth.equals(indexYearMonth)){
					titleMap.put(dataDepartMap.get("code").toString(),dataDepartMap.get("name").toString());
				}else{
					Map<String,Object> changeData = departUserChangeService.changeUserForMyReprot(indexYearMonth,dataDepartMap.get("code").toString(),dataDepartMap.get("name").toString());
					if(changeData == null){
						titleMap.put(dataDepartMap.get("code").toString(),dataDepartMap.get("name").toString());
					}else{
						titleMap.put(dataDepartMap.get("code").toString(),changeData.get("titleName").toString());
					}
				}
				dataDeparts.add(dataDepartMap.get("code").toString());
			}

		}

		/***********************************************************************
		 * @1. 权重表头(机构 或者 机构组合: 如，康复) ==> ExcelBuilderParam.TITLE_WEIGHT_DEPARTS
		 * @2. 需要展示的机构系列  ==> ExcelBuilderParam.TITLE_DATA_DEPARTStitleMap
		 */
		builderParam.addParam(ExcelBuilderParam.TITLE_WEIGHT_DEPARTS, weightDeparts);
		builderParam.addParam(ExcelBuilderParam.TITLE_DATA_DEPARTS, dataDeparts);
		builderParam.addParam(ExcelBuilderParam.TITLE_NAMES, titleMap);
		if(detailConditions != null){
			builderParam.addParam(ExcelBuilderParam.DETAIL_CONDITIONS, detailConditions);
		}
		builderParam.addParam(ExcelBuilderParam.DATA_TYPE, dataType);
		builderParam.addParam(ExcelBuilderParam.IS_TOTAL, isTotal);
		ExcelBuilderResult builderResult = builder.doExcelBuilder(builderParam);

		//表头集合
		dataMap.put(MapExcelConstants.ENTITY_LIST, builderResult.getExcelColumns()); 
		//数据集合
		dataMap.put(MapExcelConstants.MAP_LIST, builderResult.getExcelCellValues());
		return dataMap;
	}
}
